home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Cache / OutputCompression.php < prev   
PHP Script  |  2004-10-01  |  9KB  |  302 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
  16. // |          Christian Stocker <chregu@phant.ch>                         |
  17. // +----------------------------------------------------------------------+
  18.  
  19. require_once 'Cache/Output.php';
  20.  
  21. /**
  22. * Cache using Output Buffering and contnet (gz) compression.
  23. ** Usage example:
  24. *
  25. *  // place this somewhere in a central config file
  26. *  define(CACHE_STORAGE_CLASS, 'file');
  27. *  // file storage needs a dir to put the cache files
  28. *  define(CACHE_DIR, '/var/tmp/');
  29. *
  30. *  // get a cache object
  31. *  $cache = new Cache_Output(CACHE_STORAGE_CLASS, array('cache_dir' => CACHE_DIR));
  32. *
  33. *  if (!($content = $cache->start($cache->generateID($REQUEST_URI)))) {
  34. *    print "hello world";
  35. *    $cache->endPrint(+1000);
  36. *  }
  37. *  else {
  38. *    $cache->printContent();
  39. *  }
  40. *
  41. *   OR
  42. *
  43. *  if (($content = $cache->start($cache->generateID($REQUEST_URI)))) {
  44. *    $cache->printContent();
  45. *    die();
  46. *  }
  47. *    print "hello world";
  48. *    $cache->endPrint(+1000);
  49. *
  50. *
  51. * Based upon a case study from Christian Stocker and inspired by jpcache.
  52. *
  53. * @version  $Id: OutputCompression.php,v 1.7 2003/01/04 11:54:45 mj Exp $
  54. * @author   Ulf Wendel <ulf.wendel@phpdoc.de>, Christian Stocker <chregu@phant.ch>
  55. * @access   public
  56. * @package  Cache
  57. */
  58. class Cache_OutputCompression extends Cache_Output {
  59.     
  60.     /**
  61.     * Encoding, what the user (its browser) of your website accepts
  62.     * 
  63.     * "auto" stands for test using $_SERVER['HTTP_ACCEPT_ENCODING']($HTTP_ACCEPT_ENCODING).
  64.     *
  65.     * @var  string
  66.     * @see  Cache_OutputCompression(), setEncoding()
  67.     */
  68.     var $encoding = 'auto';
  69.  
  70.     
  71.     /**
  72.     * Method used for compression
  73.     *
  74.     * @var  string
  75.     * @see  isCompressed()
  76.     */ 
  77.     var $compression = '';
  78.  
  79.     
  80.     /**
  81.     * Sets the storage details and the content encoding used (if not autodetection)
  82.     * 
  83.     * @param    string  Name of container class
  84.     * @param    array   Array with container class options
  85.     * @param    string  content encoding mode - auto => test which encoding the user accepts
  86.     */    
  87.     function Cache_OutputCompression($container, $container_options = '', $encoding = 'auto') {
  88.     
  89.         $this->setEncoding($encoding);
  90.         $this->Cache($container, $container_options);
  91.         
  92.     } // end constructor
  93.  
  94.     
  95.     /**
  96.     * Call parent deconstructor.
  97.     */
  98.     function _Cache_OutputCompression() {
  99.         $this->_Cache();
  100.     } // end deconstructor
  101.     
  102.  
  103.     function generateID($variable) {
  104.         
  105.         $this->compression = $this->getEncoding();
  106.         
  107.         return md5(serialize($variable) . serialize($this->compression));
  108.     } // end generateID
  109.  
  110.     
  111.     function get($id, $group) {
  112.         $this->content = '';
  113.         
  114.         if (!$this->caching)
  115.             return '';
  116.         
  117.         if ($this->isCached($id, $group) && !$this->isExpired($id, $group))
  118.             $this->content = $this->load($id, $group);
  119.             
  120.         return $this->content;
  121.     } // end func get
  122.     
  123.     
  124.     /**
  125.     * Stops the output buffering, saves it to the cache and returns the _compressed_ content. 
  126.     *
  127.     * If you need the uncompressed content for further procession before
  128.     * it's saved in the cache use endGet(). endGet() does _not compress_.
  129.     */    
  130.     function end($expire = 0, $userdata = '') {
  131.         $content = ob_get_contents();
  132.         ob_end_clean();
  133.  
  134.         // store in the cache
  135.         if ($this->caching) {
  136.             $this->extSave($this->output_id, $content, $userdata, $expire, $this->output_group);
  137.             return $this->content;                
  138.         }
  139.             
  140.         return $content;        
  141.     } // end func end()
  142.     
  143.     
  144.     function endPrint($expire = 0, $userdata = '') {
  145.         $this->printContent($this->end($expire, $userdata));
  146.     } // end func endPrint
  147.  
  148.     
  149.     /**
  150.     * Saves the given data to the cache.
  151.     * 
  152.     */   
  153.     function extSave($id, $cachedata, $userdata, $expires = 0, $group = 'default') {
  154.         if (!$this->caching)
  155.             return true;
  156.  
  157.         if ($this->compression) {            
  158.             
  159.             $len = strlen($cachedata);            
  160.             $crc = crc32($cachedata);
  161.             $cachedata = gzcompress($cachedata, 9);
  162.             $this->content = substr($cachedata, 0, strlen($cachedata) - 4) . pack('V', $crc) . pack('V', $len);
  163.             
  164.         } else {
  165.             
  166.             $this->content = $cachedata;
  167.             
  168.         }
  169.         return $this->container->save($id, $this->content, $expires, $group, $userdata);
  170.     } // end func extSave
  171.     
  172.     /**
  173.     * Sends the compressed data to the user.
  174.     * 
  175.     * @param    string
  176.     * @access   public
  177.     */    
  178.     function printContent($content = '') {
  179.         $server = &$this->_importGlobalVariable("server");
  180.  
  181.         if ('' == $content)
  182.             $content = &$this->container->cachedata;
  183.                  
  184.         if ($this->compression && $this->caching) {
  185.    
  186.             $etag = 'PEAR-Cache-' . md5(substr($content, -40));
  187.             header("ETag: $etag");
  188.             if (isset($server['HTTP_IF_NONE_MATCH']) && strstr(stripslashes($server['HTTP_IF_NONE_MATCH']), $etag)) {
  189.                 // not modified
  190.                 header('HTTP/1.0 304');
  191.                 return;
  192.             } else {
  193.    
  194.                 // client acceppts some encoding - send headers & data
  195.                 header("Content-Encoding: {$this->compression}");
  196.                 header('Vary: Accept-Encoding');
  197.                 print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
  198.             }
  199.         
  200.         }
  201.         
  202.         die($content);
  203.     } // end func printContent
  204.     
  205.     
  206.     /**
  207.     * Returns the encoding method of the current dataset. 
  208.     *
  209.     * @access   public
  210.     * @return   string  Empty string (which evaluates to false) means no compression
  211.     */
  212.     function isCompressed() {
  213.         return $this->compression;
  214.     } // end func isCompressed
  215.  
  216.     /**
  217.     * Sets the encoding to be used.
  218.     * 
  219.     * @param    string  "auto" means autodetect for every client
  220.     * @access   public
  221.     * @see      $encoding
  222.     */
  223.     function setEncoding($encoding = 'auto') {
  224.         $this->encoding = $encoding;
  225.     } // end func setEncoding
  226.     
  227.     
  228.     /**
  229.     * Returns the encoding to be used for the data transmission to the client.
  230.     *
  231.     * @see      setEncoding()
  232.     */    
  233.     function getEncoding() {
  234.         $server = &$this->_importGlobalVariable("server");
  235.  
  236.         // encoding set by user    
  237.         if ('auto' != $this->encoding)
  238.             return $this->encoding;
  239.         
  240.         // check what the client accepts
  241.         if (false !== strpos($server['HTTP_ACCEPT_ENCODING'], 'x-gzip'))
  242.             return 'x-gzip';
  243.         if (false !== strpos($server['HTTP_ACCEPT_ENCODING'], 'gzip'))
  244.             return 'gzip';
  245.             
  246.         // no compression
  247.         return '';
  248.         
  249.     } // end func getEncoding
  250.  
  251.     // {{{ _importGlobalVariable()
  252.  
  253.     /**
  254.      * Import variables from special namespaces.
  255.      *
  256.      * @access private
  257.      * @param string Type of variable (server, session, post)
  258.      * @return array
  259.      */
  260.     function &_importGlobalVariable($variable) 
  261.     {
  262.       
  263.         $var = null;
  264.  
  265.         switch (strtolower($variable)) {
  266.  
  267.             case "server" :
  268.                 if (isset($_SERVER)) {
  269.                     $var = &$_SERVER;
  270.                 } else {
  271.                     $var = &$GLOBALS['HTTP_SERVER_VARS'];
  272.                 }
  273.                 break;
  274.  
  275.             case "session" :
  276.                 if (isset($_SESSION)) {
  277.                     $var = &$_SESSION;
  278.                 } else {
  279.                     $var = &$GLOBALS['HTTP_SESSION_VARS'];
  280.                 }
  281.                 break;
  282.  
  283.             case "post" :
  284.                 if (isset($_POST)) {
  285.                     $var = &$_POST;
  286.                 } else {
  287.                     $var = &$GLOBALS['HTTP_POST_VARS'];
  288.                 }
  289.                 break;
  290.  
  291.             default:
  292.                 break;
  293.  
  294.         }
  295.  
  296.         return $var;
  297.     } 
  298.  
  299.     // }}
  300. } // end class OutputCompression
  301. ?>
  302.